<style lang="less">
    .action-cell {
        margin-bottom: 20px;
        width: 100%;
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
        -ms-flex-pack: distribute;
        justify-content: space-between;
        align-items: center;
        margin-right: 20%;
    }

    .search-block {
        display: flex;
        margin-bottom: 10px;
    }

    .input-search {
        margin-right: 10px;
    }
</style>
<template>
    <div style="min-width: 1024px">
        <h4>子系统管理</h4>
        <Alert show-icon>
            此处用于子系统管理。
        </Alert>
        <ButtonGroup>
            <Button type="success" @click="addModal=true">
                <Icon type="md-add-circle"/>
                添加
            </Button>
            <Button type="warning" :disabled="deleteBtnEnable" @click="remove">
                <Icon type="md-close-circle"/>
                删除
            </Button>
            <Button type="info" @click="backGoJump()">
                <Icon type="md-return-left"/>
                返回门区管理
            </Button>
        </ButtonGroup>
        <Row>
            <Col span="20">
                <Table :context="self" :data="tableData" :columns="tableColumns" stripe border
                       @on-select="tableItemChecked" @on-select-cancel="tableItemChecked"
                       @on-select-all="tableItemChecked"
                       @on-select-all-cancel="tableItemChecked"
                       style="margin-top:15px;">
                </Table>
            </Col>
        </Row>
        <!--添加-->
        <Modal v-model="addModal" title="添加" @on-ok="handleSubmitAdd('formValidate')"
               @on-cancel="addModal =false">
            <Form ref="formValidate" :model="formValidate" :label-width="100">
                <Form-item label="子系统名称" prop="Subsysname">
                    <i-input v-model="formValidate.Subsysname" placeholder="请输入子系统名称"></i-input>
                </Form-item>
                <Form-item label="开门时长" prop="OpenTime">
                    <i-input v-model="formValidate.OpenTime" type="number" placeholder="请输入门开门时长"></i-input>
                </Form-item>
                <Form-item label="开门间隔时间" prop="Sametime">
                    <i-input v-model="formValidate.Sametime" type="number" placeholder="请输入开门间隔时间"></i-input>
                </Form-item>
            </Form>
        </Modal>
        <!--编辑-->
        <Modal v-model="editIsShow" title="编辑" @on-ok="handleSubmitEdit('formValidate2')"
               @on-cancel="editIsShow =false">
            <Form ref="formValidate2" :model="formValidate2" :label-width="100">
                <Form-item label="子系统名称" prop="Subsysname">
                    <i-input v-model="formValidate2.Subsysname" placeholder="请输入子系统名称"></i-input>
                </Form-item>
                <Form-item label="开门时长" prop="OpenTime">
                    <i-input v-model="formValidate2.OpenTime" type="number" placeholder="请输入门开门时长"></i-input>
                </Form-item>
                <Form-item label="开门间隔时间" prop="Sametime">
                    <i-input v-model="formValidate2.Sametime" type="number" placeholder="请输入开门间隔时间"></i-input>
                </Form-item>
            </Form>
        </Modal>

    </div>
</template>
<script>
    import apiUtil from '../../assets/utils/api_util';

    export default {
        data() {
            return {
                self: this,
                gateId: '',
                tableData: [],
                total: 0,
                current: 1,
                //添加
                formValidate: {
                    //添加字段
                    Subsysname: '',
                    Sametime: '',
                    OpenTime: '',
                },
                allDay: false,
                addModal: false,
                //编辑
                formValidate2: {
                    //添加字段
                    Subsysname: '',
                    Sametime: '',
                    OpenTime: '',
                },
                areaArr: [],
                editIsShow: false,
                //操作按钮状态管理
                deleteBtnEnable: true,
                selectedItems: [],
                //表格
                tableColumns: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '子门区系统名称',
                        key: 'Subsysname',
                        align: 'center',
                    },
                    {
                        title: '相同卡的限制时长',
                        key: 'Sametime',
                        align: 'center',
                    },
                    {
                        title: '开门时长',
                        key: 'Opentime',
                        align: 'center',
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: '',
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [

                                    h('Button', {
                                        props: {
                                            type: 'primary',
                                            size: 'small'
                                        },
                                        style: {
                                            'margin-right': '10px',
                                        },
                                        on: {
                                            click: () => {
                                                let that = this;
                                                this.editClick(params.row)
                                            }
                                        }
                                    }, '编辑'),
                                    h('Button', {
                                        props: {
                                            type: 'warning',
                                            size: 'small'
                                        },
                                        style: {
                                            'margin-right': '10px',
                                        },
                                        on: {
                                            click: () => {
                                                let that = this;
                                                this.jumpPage(params.row.Id, 'GateCtrlList')
                                            }
                                        }
                                    }, '控制器管理'),
                                ]
                            )
                        }
                    }
                ]
            }
        },
        mounted() {
            this.mockTableData();
            this.gateId = this.$route.query.id
        },
        methods: {
            //backJump
            backGoJump: function () {
                this.$router.back(-1)
            },
            jumpPage: function (id, url) {
                let route = '';
                if (id.length > 0) {
                    route = {
                        path: url,
                        query: {
                            id: id,
                        },
                    };
                } else {
                    if (id > 0) {
                        route = {
                            path: url,
                            query: {
                                id: id,
                            },
                        };
                    } else {
                        route = {
                            path: url,
                        };
                    }

                }
                this.$router.push(route);
            },
            // 表格内选中及取消选中操作
            tableItemChecked: function (selection, row) {

                this.selectedItems = selection;
                console.log('选择删除项:', selection);

                if (this.selectedItems.length > 1) {
                    // 此处添加不要多选的提示
                    this.$Message.warning('此处不允许批量删除 ');
                    this.deleteBtnEnable = true;

                } else {
                    this.deleteBtnEnable = false;
                }
            },
            //删除
            remove: function () {
                let self = this;
                let that = this;
                this.$Modal.confirm({
                    title: '操作警告',
                    content: '即将删除选中的子系统项，该操作不可逆转，请确认您的操作。',
                    onOk: function () {
                        this.$Loading.start();
                        var selectedItemIds = "";
                        for (var i = 0; i < that.selectedItems.length; i++) {
                            selectedItemIds += that.selectedItems[i].Id.toString() + ",";
                        }
                        selectedItemIds = selectedItemIds.substring(0, selectedItemIds.length - 1);
                        let para = {id: selectedItemIds};
                        let params = that.$qs.stringify(para);

                        apiUtil.post(that, apiUtil.urls.DelGateSubSys, params, function (res) {
                            that.$Message.success('删除成功。');
                            that.mockTableData();
                        });
                    }
                });
            },
            //添加
            handleSubmitAdd(name) {
                let that = this;
                let model = this.$refs[name].model;
                this.$refs[name].validate((valid) => {
                    that.addData(model);
                });
            },
            addData: function (model) {
                let that = this;

                model.Forgateid = that.$route.query.id;
                let params = that.$qs.stringify(model);
                this.$Loading.start();

                console.log('params', model);

                apiUtil.post(that, apiUtil.urls.AddGateSubSys, params, function (res) {
                    that.$Loading.finish();
                    that.addModal = false;
                    that.mockTableData();
                    that.$Message.info('添加成功 ')
                    that.handleReset('formValidate')

                });
            },
            //点击编辑
            editClick: function (row) {
                this.editIsShow = true;
                this.formValidate2.OpenTime = row.Opentime;
                this.formValidate2.Sametime = row.Sametime;
                this.formValidate2.Subsysname = row.Subsysname;
                this.formValidate2.Id = row.Id;
            },
            handleSubmitEdit(name) {
                let that = this;
                let model = this.$refs[name].model;
                this.$refs[name].validate((valid) => {
                    that.comfirmModifyPSEdit(model);
                })
            },
            comfirmModifyPSEdit: function (model) {
                let that = this;
                let params = {};

                params = that.$qs.stringify(model);
                console.log('params:', params)
                apiUtil.post(that, apiUtil.urls.EditGateSubSys, params, function (res) {
                    that.$Message.info('修改成功 ')
                    that.mockTableData();

                });
            },
            mockTableData() {
                let that = this;

                let para = {
                    gateid: that.$route.query.id,
                };
                let params = that.$qs.stringify(para);

                this.$Loading.start();

                apiUtil.get(that, apiUtil.urls.GateSubSysList, para, function (res) {
                    that.$Loading.finish();

                    that.tableData = res.Msg;

                });
            },
            //重置表单
            handleReset (name) {
                this.$refs[name].resetFields();
            }
        },
    }
</script>
